package com.rt.tmpt.utils;

import java.io.File;
import java.io.IOException;
import java.util.*;


/**
 * 一些工具类
 * @author wuxiaoyong
 *
 */
public class Utils { 
	/**
	 * 拼接Exception信息成，字符串
	 * @param e
	 * @return 异常信息字符串形式
	 */
	public static String printException(Exception e){
		StringBuffer sb = new StringBuffer();
		sb.append(e.toString());
		sb.append("\r\n");
		StackTraceElement[] exceptions = e.getStackTrace();
		for(int i=0;i<exceptions.length;i++){
			StackTraceElement s = exceptions[i];
			sb.append(s);
			sb.append("\r\n");
		}
		String result = sb.toString();
		return result;
	}
	
	/** 10进制转16进制 **/
	public static byte[] dec2Hex(int data){
		byte[] bBuffer = new byte[2];
		bBuffer[0] = (byte) (data >> 8 & 0xFF);
		bBuffer[1] = (byte) (data & 0xFF);
		//String hex = Integer.toHexString(dec);
		return bBuffer;
	}
	
	
	/**
	 * 填充0，0在左边
	 * @param data:填充的数据
	 * @param num: 0的总个数
	 * @return: 填充后的结果
	 */
	public static String fillDataWith0(String data, int num){
		if(data.length()>num){
			data = data.substring(data.length()-num);
		}
		StringBuffer sb0 = new StringBuffer();
		for(int i=0;i<num;i++){
			sb0.append("0");
		}
		StringBuffer sb = new StringBuffer(sb0.toString());
		sb.replace(num-data.length(), num, data);
		return sb.toString();
	}

	   /**
	     * 十六进制字符串转二进制
	     * @param str 十六进制串
	     * @return
	     */
	    public static byte[] hex2byte(String str) { //字符串转二进制
	        int len = str.length();
	        String stmp = null;
	        byte bt[] = new byte[len / 2];
	        for (int n = 0; n < len / 2; n++) {
	            stmp = str.substring(n * 2, n * 2 + 2);
	            bt[n] = (byte) (Integer.parseInt(stmp, 16));
	        }
	        return bt;
	    }
	    
	   /**
	     * 二进制转十六进制字符串
	     * @param b
	     * @return
	     */
	    public static String byte2hex(byte[] b) { //二行制转字符串
	        String hs = "";
	        String stmp = "";
	        for (int n = 0; n < b.length; n++) {
	            stmp = (Integer.toHexString(b[n] & 0XFF));
	            if (stmp.length() == 1) {
	                hs = hs + "0" + stmp;
	            } else {
	                hs = hs + stmp;
	            }
	            if (n < b.length - 1) {
	                hs = hs + "";
	            }
	        }
	        return hs.toUpperCase();
	    }
	    
	    
	    public static File genFile(String fileFullName) throws IOException{
			File file = new File(fileFullName);
			if(!file.exists()){
				file.getParentFile().mkdirs();
				if(!file.createNewFile())	throw new IOException("can not create file");
			}
			return file;
	    }
	    
	    public static String space2Null(Object o){
	    	if(o==null)return null;
	    	if("".equals(o.toString().trim())){
	    		return null;
	    	}
	    	return o.toString().trim();
	    }
	    
	    @SuppressWarnings("unchecked")
		public static String sortNumber(String data, String split1, String split2){
	    	if(data==null || data.equals("")){
	    		return data;
	    	}
			String[] productSonPositionArr = data.split(split1);
			List<String> productSonPositionList = Arrays.asList(productSonPositionArr);
			Collections.sort(productSonPositionList, new Comparator() {
				public int compare(Object o1, Object o2) {
					int n1 = Integer.parseInt(o1.toString());
					int n2 = Integer.parseInt(o2.toString());
					if (n1 > n2) return 1;
					else if (n1 < n2) return -1;
					return 0;
				}
			});
			StringBuffer sb = new StringBuffer();
			for(String s: productSonPositionList){
				sb.append(s).append(split2);
			}
			return sb.toString();
	    }
	    
	    
	    public static List<String> split(String data, String split1){
	    	if(data==null || data.equals("")){
	    		return null;
	    	}
			String[] dataArr = data.split(split1);
			List<String> dataList = new ArrayList<String>(Arrays.asList(dataArr));
			dataList.remove(" ");
			
			return dataList;
	    }

	    /**
	     * 密码通用校验
	     * @param password
	     * @param minLen
	     * @param maxLen
	     * @param loginname
	     * @return
		public static Result okPwdComplexCheck(String password, int minLen, int maxLen, String loginname){
			Result result = new Result();
			String msg = null;
			
			if(password==null || password.trim().equals("")){
				msg="密码不能为空";
	    		return result.fault(msg);
			}
			
			int index = password.indexOf(loginname);
	    	if(index > -1){
	    		msg="密码不能包含用户名";
	    		return result.fault(msg);
	    	}
			
	    	if(!okPwdComplexCheck(password, minLen, maxLen)){
	    		msg = "密码:不满足密码复杂性要求";
	    		return result.fault(msg);
	    	}
			return result.success();
		}
		 */


	    /**
	     * 密码复杂度
	     * @param password
	     * @param minLen
	     * @param maxLen
	     * @return
	     */
		public static boolean okPwdComplexCheck(String password, int minLen, int maxLen){
	    	
	    	if(password.length()<minLen || password.length()>maxLen){
	    		return false;
	    	}
	    	char firstChar = password.charAt(0);
	    	
	    	if(firstChar>='0' && firstChar<='9'){
	    		int count=0;
	    		for(int i=0;i<password.length();i++){
	    			char newChar = password.charAt(i);
	    			if(newChar>='0' && newChar<='9'){
	    				count++;
	    			}
	    		}
	    		if(count==password.length()){
	    			return false;
	    		}
	    	}
	    	

	    	if(firstChar>='a' && firstChar<='z'){
	    		int count=0;
	    		for(int i=0;i<password.length();i++){
	    			char newChar = password.charAt(i);
	    			if(newChar>='a' && newChar<='z'){
	    				count++;
	    			}
	    		}
	    		if(count==password.length()){
	    			return false;
	    		}
	    	}

	    	if(firstChar>='A' && firstChar<='Z'){
	    		int count=0;
	    		for(int i=0;i<password.length();i++){
	    			char newChar = password.charAt(i);
	    			if(newChar>='A' && newChar<='Z'){
	    				count++;
	    			}
	    		}
	    		if(count==password.length()){
	    			return false;
	    		}
	    	}
			return true;
		}
		
		public static String nullValue(String value){
			return value==null?"":value;
		}
		
	    public static void main1(String[] args) {
			List<String> ss = Utils.split("1,1, ", ",");
			System.out.println(ss.size());
			System.out.println(Utils.okPwdComplexCheck("laoliou123",6,40));
		}
	    
	    
	    
}
